package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.service.OrderSettingService;
import constant.MessageConstant;
import entity.Result;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import pojo.OrderSetting;
import utils.POIUtils;

import javax.print.attribute.standard.MediaSize;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 预约设置
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
    
    @Reference
    private OrderSettingService orderSettingService;
    
    /**
     * 进行预约设置,通过Excel文件上传，并解析文件内容保存到数据库
     */
    @RequestMapping("/upload.do")
    public Result upload(@RequestParam("excelFile")MultipartFile excelFile){
        try {
            //读取Excel文件数据,返回list集合类型的数据,list装的是数组，每个数组是一行的数据,一行数据其实有两列(日期，预约人数)
            List<String[]> list = POIUtils.readExcel(excelFile);
            
            if(list != null && list.size() > 0){
                //创建集合，存储OrderSetting对象，Excel文件中有多少行数据,就会封装多少个OrderSetting对象并添加到这个集合
                List<OrderSetting> orderSettings = new ArrayList<>();
                
                //遍历list集合中的数组(每行数据)，封装为OrderSetting对象
                for (String[] row : list){
                    //在这里要注意，row[0],或row[1]不存在的话，会报错，所有要保证excel表每一行两项都必须填
                    OrderSetting orderSetting = new OrderSetting(new Date(row[0]), Integer.parseInt(row[1]));
                    
                    orderSettings.add(orderSetting);
                }
                orderSettingService.add(orderSettings);
                
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    
    }
    
    /**
     * 根据日期查询预约设置,(获取指定日期所在月份的预约设置数据)
     * getOrderSettingByMonth.do?date=" + this.currentYear + "-" + this.currentMonth)
     */
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date){
        System.out.println("根据日期查询预约设置,(获取指定日期所在月份的预约设置数据)================");
        //date: 2022-09
        try{
            List<Map> list = orderSettingService.getOrderSettingByMonthgetOrderSettingByMonth(date);
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
    }
    
    /**
     *进行预约设置(添加预约设置或修改已存在预约设置)
     */
    @RequestMapping("/editNumberByDate")
    //前端过来的有number，orderDate
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        try {
            orderSettingService.editNumberByDate(orderSetting);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.ORDERSETTING_FAIL);
    
    }
}
